package org.szy.core.common.interceptor;

import org.noear.solon.Solon;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.handle.Context;
import org.noear.solon.validation.annotation.NoRepeatSubmit;
import org.noear.solon.validation.annotation.NoRepeatSubmitChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.szy.core.common.cache.CacheOperator;

/**
 * 分布式，重复提交数据校验
 *
 * @author 孙志阳
 * @date 2022-10-10
 * @since 1.0.0
 **/
@Component
public class NoRepeatSubmitCheckerImpl implements NoRepeatSubmitChecker {

    Logger log = LoggerFactory.getLogger(NoRepeatSubmitCheckerImpl.class);
    @Inject
    private CacheOperator cacheOperator;

    @Override
    public boolean check(NoRepeatSubmit anno, Context ctx, String submitHash, int limitSeconds) {
        //借用分布式锁，挡住 submitHash 在一定时间内多次进入
        return cacheOperator.tryLock(Solon.cfg().appName(), submitHash, limitSeconds);
    }
}
